package login;

import java.util.UUID;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.EJB;
import javax.ejb.Remove;
import javax.ejb.Stateful;

import manager.UserManagerRemote;
import entity.Utilisateur;

@Stateful
public class LoginBean implements LoginRemote {
    
	// valeurs stockées ds le stateful bean

	public static final String RemoteJNDIName =  LoginBean.class.getSimpleName() + "/remote";
	private String id;
	private String session_login;
	private String session_motDePasse;
	private String session_nom;
	private String session_prenom;
	private String session_adresse;
	private String session_email;
	  
    // Bean UserManager
	
	@EJB
    private UserManagerRemote user_manager;
    
	@PostConstruct
	public void initialize() {
		   
		 System.out.println("cb:construct()");
		    
		   UUID uuid = UUID.randomUUID();
		   id = uuid.toString();
		   
		   session_login="";
		   session_motDePasse="";
		   session_nom="";
		   session_prenom="";
		   session_adresse="";
		   session_email="";
		}
	
   @PreDestroy
   public void destroy(){
		System.out.println("cb:destroy()");
	}
   
    public boolean login(String username,String mdp){
        
   	
    	 Utilisateur u = user_manager.check(username,mdp);
    	 
    	 if (u!=null )
    	 {
    		session_login=u.getLogin();
    		session_motDePasse=u.getMotDePasse();
    		session_nom=u.getNom();
    		session_prenom=u.getPrenom();
    		session_adresse=u.getAdresse();
    		session_email=u.getEmail();
    		return true;
    	 
    	 }
    	 else
    	 {
    		 System.out.println("User not found!!!");
    		 return false;
    	 }    	    	
    }

    
   public String getInfos(){
	   
		return  "UUID: " + id
		+ "\n" + session_login
		+ "\n" + session_motDePasse
		+ "\n" + session_nom
		+ "\n" + session_prenom
		+ "\n" + session_adresse
		+ "\n" + session_email;
   		}
    
   @Remove
    public void logoff() 
    {
	   session_login="";
	   session_motDePasse="";
	   session_nom="";
	   session_prenom="";
	   session_adresse="";
	   session_email="";	
   }
    
    public String getLogin()
    {
    	return this.session_login;
    }
    
    public void setUser(Utilisateur u)
    {
       session_login=u.getLogin();
 	   session_motDePasse=u.getMotDePasse();
 	   session_nom=u.getNom();
 	   session_prenom=u.getPrenom();
 	   session_adresse=u.getAdresse();
 	   session_email=u.getEmail();	
    	
    }
	    
} 
